package com.starlight.ts_clouddev.controller;


import com.starlight.ts_clouddev.annotition.AuthenticationCheck;
import com.starlight.ts_clouddev.annotition.RoleType;
import com.starlight.ts_clouddev.command.CloudUserRegisterCommand;
import com.starlight.ts_clouddev.infrastructure.entity.CloudUser;
import com.starlight.ts_clouddev.service.CloudUserService;
import com.starlight.ts_clouddev.service.captcha.CaptchaService;
import com.starlight.ts_clouddev.util.CloudResponse;
import com.starlight.ts_clouddev.util.CurrentUserInfoUtils;
import jakarta.annotation.Resource;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yfy2001
 * @date 2023/4/13 14:25
 */
@RestController
@RequestMapping("/user")
public class CloudUserController {
    @Resource
    private CloudUserService cloudUserService;

    @Resource
    private CaptchaService captchaService;

    @PostMapping ("/register")
    public CloudResponse<Integer> register(CloudUserRegisterCommand command){
        System.out.println(command.toString());
        if (!captchaService.verifyCaptcha(command.getEmail(),command.getCaptcha())){
            throw new RuntimeException("验证码错误！");
        }
        CloudUser register=new CloudUser();
        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
        register.setEmail(command.getEmail());
        register.setPassword(bCryptPasswordEncoder.encode(command.getPassword()));
        return CloudResponse.success(cloudUserService.userRegister(register));
    }

    @PostMapping("/update")
    public CloudResponse<Boolean> update(CloudUser cloudUser){
        return CloudResponse.success(cloudUserService.updateById(cloudUser));
    }

    @GetMapping("/read")
    public CloudResponse<List<CloudUser>> read(){
        return CloudResponse.success(cloudUserService.list());
    }

    @GetMapping("/readCurrentUserInfo")
    @AuthenticationCheck(roleType = {RoleType.LOGIN})
    public CloudResponse<Map<String,String>> readCurrentUserInfo(){
        CloudUser cloudUser=cloudUserService.getById(Long.valueOf(CurrentUserInfoUtils.getUserId()));
        Map<String,String> userInfoMap=new HashMap<>();
        userInfoMap.put("userId", CurrentUserInfoUtils.getUserId());
        userInfoMap.put("account", String.valueOf(cloudUser.getAccount()));
        return CloudResponse.success(userInfoMap);
    }

    @PostMapping("/delete")
    public CloudResponse<Boolean> delete(CloudUser cloudUser){
        return CloudResponse.success(cloudUserService.removeById(cloudUser));
    }

    /**
     * 模拟用户充值
     * @param amount 充值金额
     * @return 是否成功
     */
    @PostMapping("/rechargeAccount")
    @AuthenticationCheck(roleType = {RoleType.LOGIN})
    public CloudResponse<Boolean> rechargeAccount(Long amount){
        CloudUser cloudUser=cloudUserService.getById(Long.valueOf(CurrentUserInfoUtils.getUserId()));
        Long oldAccount= cloudUser.getAccount();
        Long newAccount=oldAccount+amount;
        cloudUser.setAccount(newAccount);
        return CloudResponse.success(cloudUserService.updateById(cloudUser));
    }

}
